# 简单测试文件类型GET API的PowerShell脚本

# 设置编码以避免乱码
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[System.Text.Encoding]::Default = [System.Text.Encoding]::UTF8

$apiUrl = "http://localhost:5269/api/file/types"

Write-Host "正在调用API: $apiUrl"

try {
    # 调用API获取所有文件类型
    $response = Invoke-RestMethod -Uri $apiUrl -Method Get -ContentType "application/json"
    
    Write-Host "API调用成功!" -ForegroundColor Green
    Write-Host "返回的文件类型数量: $($response.Count)"
    Write-Host "\n文件类型详情:"
    
    # 遍历并显示每个文件类型的详细信息
    for ($i = 0; $i -lt $response.Count; $i++) {
        $type = $response[$i]
        Write-Host "$($i+1). ID: $($type.id), TypeName: $($type.typeName), MemoryStrength: $($type.memoryStrength)"
    }
    
    # 将完整响应转换为JSON并格式化输出
    Write-Host "\n完整JSON响应:"
    $response | ConvertTo-Json -Depth 10 -Compress:$false
} catch {
    Write-Host "API调用失败!" -ForegroundColor Red
    Write-Host "错误信息: $($_.Exception.Message)"
    if ($_.Exception.Response) {
        $statusCode = $_.Exception.Response.StatusCode.value__
        $statusDescription = $_.Exception.Response.StatusDescription
        Write-Host "状态码: $statusCode ($statusDescription)"
        # 尝试读取错误响应内容
        try {
            $errorContent = $_.Exception.Response.GetResponseStream() | 
                            New-Object System.IO.StreamReader([System.Text.Encoding]::UTF8) | 
                            ReadToEnd()
            Write-Host "错误响应内容: $errorContent"
        } catch {
            Write-Host "无法读取错误响应内容"
        }
    }
}